﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Warensoft.Unity.Communication.Client.Models;

namespace Warensoft.Unity.Communication.Client.OData
{
    public class ODataTable : IEnumerable
    {
        private HttpClient httpClient;

        public string TableName { get; set; }
        public string TableUrl { get; set; }
        public List<ODataEntity> Rows { get; set; }
        public List<ODataProperty> PropertDefinations { get; set; }
        public List<ODataNavigation> NavigationDefinations { get; set; }
        private bool isNavigationTable = false;
        private ODataContext currentContext;
        public ODataTable(string tableName, string tableUrl, bool isNavigationTable, List<ODataProperty> propertyDefinations,List<ODataNavigation>navigationDefinations,ODataContext context)
        {
            this.currentContext = context;
            this.isNavigationTable = isNavigationTable;
            this.TableName = tableName;
            this.TableUrl = tableUrl;
            this.PropertDefinations = propertyDefinations;
            this.NavigationDefinations = navigationDefinations;
            this.httpClient = HttpClient.CreateInstance();
        }
        protected void FillAsync(Action fillAsyncCompleted, string queryUrl)
        {
            
            this.httpClient.BeginGetHttpContent(queryUrl, new Action<System.Xml.XmlDocument>((doc) =>
            {

                XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
                nsmgr.AddNamespace("warensoft", "http://www.w3.org/2005/Atom");
                nsmgr.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");
                nsmgr.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
                this.Rows = doc.SelectNodes("//warensoft:entry", nsmgr).Cast<XmlNode>().Select(entry => new ODataEntity(entry, nsmgr,this.PropertDefinations,this.NavigationDefinations,this.currentContext )).ToList();
                fillAsyncCompleted();

            }));
        }
        public void FillAsync(Action fillAsyncCompleted, ODataQuery query)
        {
            if (this.isNavigationTable)
            {
                throw new Exception("conditional query is not supported in navigation table");
            }
            this.FillAsync(fillAsyncCompleted,query.ToString ());
        }
        public void FillAsync(Action fillAsyncCompleted)
        {
            this.FillAsync(fillAsyncCompleted, this.TableUrl);
        }
        


        public Type ElementType
        {
            get { throw new NotImplementedException(); }
        }

        public System.Linq.Expressions.Expression Expression
        {
            get { throw new NotImplementedException(); }
        }

        public IQueryProvider Provider
        {
            get { throw new NotImplementedException(); }
        }

        public System.Collections.IEnumerator GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }
}
